/**
 * 转动抽奖
 *
 * @author jinguangguo & luzhaoman
 * @date 2012.9
 */

//转盘
$('#disk-start').die('click').live('click', function () {

    if ($(this).data('isTurning') === undefined) {
        
        var that = this;
        var url = ajaxUrlPrefix+"/draw/checkLogin.do";
        $.ajax({
             dataType:"jsonp",
             jsonp:"jsonpcallback",
             url:url,
             success: function(data){
                 //判断是否登录
                 if(data.value == 1){
                     $.ajax({
                           dataType:"jsonp",
                           jsonp:"jsonpcallback",
                           url:ajaxUrlPrefix+"/draw/playDraw.do?activeId="+activeId,
                           success: function(jsonp){
                               data=jsonp.value;
                               if(data.indexOf("success")>-1){
                                        //封锁
                                        $(that).data('isTurning', 'true');
                                        var arr = data.split(":");
                                        var stopIndex=arr[1];
                                        var awardType=arr[2];
                                        $(that).closest('table').turn({
                                            btn: that,
                                            stopIndex: stopIndex,
                                            errorFlag: false,
                                            callback: function () {
                                                //解锁
                                                $(that).removeData('isTurning');
                                                switch(awardType+""){
                                                case '0': //没有中奖

                                                     diskTips({
                                                         type: 'fail',
                                                         gnum: '10G',
                                                         callback: function () {
                                                               $.XYTipsWindow.removeBox();                                                                  
                                                         }
                                                     });
                                                     break;
                                                     
                                                case '1': //实物中奖
                                                    var jsonStr=data.split(":#")[1];
                                                    var json= jQuery.parseJSON(jsonStr);
                                                    var winnerId=arr[4];
                                                    diskTips({
                                                            type: 'getAward',
                                                            name: arr[3],
                                                            realName: json.userTrueName,
                                                            phone: json.mobilephone,
                                                            qq: json.qqNum,
                                                            addressCode: '',
                                                            address:json.detailedAddress,
                                                            province: json.province,
                                                            city: json.city,
                                                            callback: function () {
                                                                
                                                                if(!chkUserContact()){
                                                                    $('.tips-validate').text('请完善资料！').show();
                                                                    return false;
                                                                }
                                                                var realName = $("#contactDiv").find('input[name=realName]').val().trim();
                                                                var phone=$("#contactDiv").find('input[name=phone]').val().trim();
                                                                var qq=$("#contactDiv").find('input[name=qq]').val().trim();
                                                                var addressCode=$("#contactDiv").find('input[name=addressCode]').val().trim();
                                                                var province=$("#contactDiv").find(':selected').first().text();
                                                                var city=$("#contactDiv").find(':selected').last().text();
                                                                var address=$("#contactDiv").find('textarea[name=address]').val().trim();
                                                                $.ajax({
                                                                    type: "POST",
                                                                    url:ajaxUrlPrefix+"/draw/setUserContract.do?activeId="+activeId,
                                                                    data:"winnerId="+winnerId+
                                                                    "&realName="+realName
                                                                    +"&phone="+phone                                                                   
                                                                    +"&addressCode="+addressCode
                                                                    +"&address="+address
                                                                    +"&province="+province
                                                                    +"&city="+city
                                                                    +"&qq="+qq,
                                                                       success: function(data){
                                                                           if(data == 1){
                                                                               $.XYTipsWindow.removeBox();
                                                                               showTips('保存成功');
                                                                           }else{
                                                                               showTips('对不起，保存失败！');
                                                                           }
                                                                       }
                                                                });
                                                            }
                                                     });
    
                                                    var province = $('.tips-wrapper').find("select[name='province']");
                                                    province.unbind('change').bind('change', function () {
                                                        var $city = $(this).siblings("select[name='city']");
                                                        SetCity(this, $city[0]);
                                                    });
                                                    
                                                     break;
                                                case '2': //虚拟奖品中奖
                                                    var cardno=arr[4];
                                                    var cardpass='';
                                                    if(arr.length>=6){
                                                        cardpass=arr[5];
                                                    }
                                                    var vtype="getGoldWithCardInfo";
                                                    if(cardpass==""||cardpass==null){
                                                        vtype="getGoldWithCardNo";
                                                    }
                                                    
                                                    diskTips({
                                                        type:vtype,
                                                        name: arr[3],
                                                        cardno: cardno,
                                                        cardpass: cardpass,
                                                        callback: function () {
                                                            $.XYTipsWindow.removeBox();
                                                        }
                                                    });
                                                     break;
                                                case '3': //G币
                                                    diskTips({
                                                        type: 'getGold',
                                                        gnum: arr[3],
                                                        callback: function () {
                                                            $.XYTipsWindow.removeBox();
                                                        }
                                                    });
                                                     break;
                                                case '4': //G币
                                                    diskTips({
                                                        type: 'getExp',
                                                        exp: arr[3],
                                                        callback: function () {
                                                            $.XYTipsWindow.removeBox();
                                                        }
                                                    });
                                                     break; 
                                                default:     
                                                }
                                                return;
                                            }
                                        });
                                        return ;
                                }
                                if(data.indexOf("DrawException")>-1){
                                    var errorCode = data.split(":")[1];
                                    if(errorCode==100){ //活动结束了
                                        showTips('对不起，活动结束了!',function() {
                                            activeFinished();
                                        });
                                        return;
                                    }
                                    if(errorCode==101){ //基本信息完整度不够
                                         diskTips({
                                             type: 'noBaseInfo'
                                         });
                                        return;
                                    }
                                    if(errorCode==102){ //联系信息完整度不够
                                         diskTips({
                                             type: 'noContactInfo'
                                         });
                                        return;
                                    }
                                    if(errorCode==103){ //级别不够
                                        diskTips({
                                             type: 'noSiteGrade',
                                             callback: function () {
                                                 $.XYTipsWindow.removeBox();
                                                 $('#XYTipsWindowBg').css('z-index', $('#XYTipsWindowBg').css('z-index') - 1);
                                             }
                                         });
                                        return;
                                    }
                                    if(errorCode==104){ //G币不足
                                        diskTips({
                                             type: 'noGold',
                                             callback: function () {
                                                 $.XYTipsWindow.removeBox();
                                             }
                                         });
                                        return;
                                    }
                                    if(errorCode==105){ //抽奖次数用尽
                                        diskTips({
                                             type: 'noPlayNum',
                                             callback: function () {
                                                 $.XYTipsWindow.removeBox();
                                             }
                                         });
                                        return;
                                    }
                                    showTips('对不起，抽奖出错了');
                                    return;
                                }
                                if(data.indexOf("error")>-1){
                                    showTips('对不起，抽奖出错了');
                                    return;
                                }else{
                                    showTips('对不起，抽奖出错了');
                                    return;
                                }
                           }
                        });
                 }else{
                    //弹出登录窗口
                    showLoginDialog(function() {
                        window.location.reload();
                    });
                    return;
                 }
             }
        });
    }
}); //end 转盘

String.prototype.trim=function(){
    return this.replace(/(^\s*)|(\s*$)/g, "");
}